package com.yvon.maple.service.system.feign;

import com.yvon.boot.common.result.ResponseData;
import com.yvon.maple.cloud.api.system.dto.SecurityUserDTO;
import com.yvon.maple.cloud.api.system.vo.LdapUserVo;
import com.yvon.maple.cloud.web.data.ApiController;
import com.yvon.maple.constants.SystemConstant;
import com.yvon.maple.enums.UserTypeEnum;
import com.yvon.maple.service.system.entity.User;
import com.yvon.maple.service.system.service.UserService;
import com.yvon.maple.utils.DataConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import static com.yvon.maple.service.system.feign.UserFeignController.PATH;


/**
 * @author Yvon
 * @since 2023/1/5
 */
@RestController
@RequestMapping(value = PATH)
public class UserFeignController extends ApiController {

    @Autowired
    private UserService userService;

	public static final String PATH = SystemConstant.Common.FEIGN_INSIDE_URL_PREFIX + "/users";

	@GetMapping("/security/username/{username}")
    public ResponseData<SecurityUserDTO> getByUsername(@PathVariable("username") String username) {
        return success(userService.loadByUsername(username));
    }

	@GetMapping("/security/mobile/{mobile}")
    public ResponseData<SecurityUserDTO> getByMobile(@PathVariable("mobile") String mobile) {
        User user = userService.getByMobile(mobile);
        return success(DataConverter.convert(user, SecurityUserDTO.class));
    }

	@GetMapping("/security/email/{email}")
    public ResponseData<SecurityUserDTO> getByEmail(@PathVariable("email") String email) {
        User user = userService.getByEmail(email);
        return success(DataConverter.convert(user, SecurityUserDTO.class));
    }

	@PostMapping("/ldap/save")
    public ResponseData<Boolean> saveLdapUser(@RequestBody LdapUserVo ldapUserVo) {
        User byUsername = userService.getLdapByUsername(ldapUserVo.getUsername());
        if (Objects.isNull(byUsername)) {
            User user = DataConverter.convert(ldapUserVo, User.class);
            user.setUserType(UserTypeEnum.LDAP);
            userService.save(user) ;
        }
        return success(true);
    }

	@PostMapping("/msg/userId/email")
    public ResponseData<Set<String>> getEmailByUserIds(@RequestBody Set<Long> userIds) {
        return success(userService.getEmailByUserIds(userIds));
    }

	@PostMapping("/msg/userId/mobile")
    public ResponseData<Set<String>> getMobileByUserIds(@RequestBody Set<Long> userIds) {
        return success(userService.getMobileByUserIds(userIds));
    }

	@PostMapping("/msg/roleId/userId")
    public ResponseData<Set<String>> getUserIdsByRoleIds(@RequestBody Set<Long> roleIds) {
        return success(userService.getUserIdsByRoleIds(roleIds).stream().map(String::valueOf).collect(Collectors.toSet()));

    }
	@PostMapping("/msg/roleId/email")
    public ResponseData<Set<String>> getEmailByRoleIds(@RequestBody Set<Long> roleIds) {
        return success(userService.getEmailByRoleIds(roleIds));
    }

	@PostMapping("/msg/roleId/mobile")
    public ResponseData<Set<String>> getMobileByRoleIds(@RequestBody Set<Long> roleIds) {
        return success(userService.getMobileByRoleIds(roleIds));
    }
}
